/**
 * Notes:会场模块后台管理-控制器
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2022-06-23 10:20:00 
 */

const BaseProjectAdminController = require('./base_project_admin_controller.js');
const AdminRoomService = require('../../service/admin/admin_room_service.js');
const timeUtil = require('../../../../framework/utils/time_util.js');
const contentCheck = require('../../../../framework/validate/content_check.js');
const RoomModel = require('../../model/room_model.js');

class AdminRoomController extends BaseProjectAdminController {

	/** 状态修改 */
	async statusRoom() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			id: 'must|id',
			status: 'must|int',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new AdminRoomService();
		return await service.statusRoom(input.id, input.status);
	}

	/** 列表 */
	async getRoomList() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			search: 'string|min:1|max:30|name=搜索条件',
			sortType: 'string|name=搜索类型',
			sortVal: 'name=搜索类型值',
			orderBy: 'object|name=排序',
			whereEx: 'object|name=附加查询条件',
			page: 'must|int|default=1',
			size: 'int',
			isTotal: 'bool',
			oldTotal: 'int',
		};

		// 取得数据
		let input = this.validateData(rules);

		let adminService = new AdminRoomService();
		let result = await adminService.getRoomList(input);

		// 数据格式化
		let list = result.list;
		for (let k = 0; k < list.length; k++) {
			list[k].ROOM_ADD_TIME = timeUtil.timestamp2Time(list[k].ROOM_ADD_TIME, 'Y-M-D h:m:s');
			list[k].ROOM_EDIT_TIME = timeUtil.timestamp2Time(list[k].ROOM_EDIT_TIME, 'Y-M-D h:m:s');
			if (list[k].ROOM_OBJ && list[k].ROOM_OBJ.desc)
				delete list[k].ACTIVITY_OBJ.desc;
		}
		result.list = list;
		return result;
	}

	/** 发布 */
	async insertRoom() {
		await this.isAdmin();

		// 数据校验 
		let rules = {
			title: 'must|string|min:2|max:50|name=标题',
			// cateId: 'must|string|name=分类',
			// cateName: 'must|string|name=分类名称',
			order: 'must|int|min:0|max:9999|name=排序号',
			seatCnt: 'must|int|name=会场座位数量',
			seatList: 'must|array|name=会场座位列表',
			address: 'must|string|name=会场地点',
			addressGeo: 'must|object|name=会场地点GEO',
			forms: 'must|array|name=会场表单',
		};

		// 取得数据
		let input = this.validateData(rules);

		// 内容审核
		await contentCheck.checkTextMultiAdmin(input);

		let service = new AdminRoomService();
		let result = await service.insertRoom(input);
		this.logOther('添加了会场《' + input.title + '》');
		return result;
	}

	/** 获取信息用于编辑修改 */
	async getRoomDetail() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			id: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new AdminRoomService();
		let room = await service.getRoomDetail(input.id);
		return room;
	}

	/** 编辑 */
	async editRoom() {
		await this.isAdmin();
		let rules = {
			id: 'must|id',
			title: 'must|string|min:2|max:50|name=标题',
			seatCnt: 'must|int|name=会场座位数量',
			seatList: 'must|array|name=会场座位列表',
			address: 'must|string|name=会场地点',
			addressGeo: 'must|object|name=会场地点GEO',
			order: 'must|int|min:0|max:9999|name=排序号',
			forms: 'must|array|name=表单'
		};

		// 取得数据
		let input = this.validateData(rules);

		// 内容审核
		await contentCheck.checkTextMultiAdmin(input);

		let service = new AdminRoomService();
		let result = service.editRoom(input);
		this.logOther('修改了会场《' + input.title + '》');
		return result;
	}

	async clearRoomAll() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			id: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new AdminRoomService();
		return await service.clearRoomAll(input.id);
	}

	/** 删除 */
	async delRoom() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			id: 'must|id',
		};

		// 取得数据
		let input = this.validateData(rules);
		let service = new AdminRoomService();
		await service.delRoom(input.id);
	}

	/** 更新图片信息 */
	async updateRoomForms() {
		await this.isAdmin();

		// 数据校验
		let rules = {
			id: 'must|id',
			hasImageForms: 'array'
		};

		// 取得数据
		let input = this.validateData(rules);

		// 内容审核
		await contentCheck.checkTextMultiAdmin(input);

		let service = new AdminRoomService();
		return await service.updateRoomForms(input);
	}
}

module.exports = AdminRoomController;